package com.csap.framework.oauth2;

import com.csap.framework.oauth2.FindUserService;
import com.csap.framework.oauth2.UserInfo;
import com.csap.framework.oauth2.UserInfoMod;
import com.csap.framework.util.map.CsapMap;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;

/**
 * 用户策略模式上下文
 *
 * @author yangchengfu
 * @dataTime 2021年-07月-27日 23:08:00
 **/
public class UserInfoStrategyContext implements ApplicationContextAware {
    /**
     * 用户信息策略
     */
    private static final Map<UserInfoMod, FindUserService> USER_INFO = CsapMap.build();

    /**
     * 根据策略类型获取服务
     *
     * @param userInfoModEnum 用户信息模式
     * @return 用户信息操作服务
     */
    public static FindUserService userService(UserInfoMod userInfoModEnum) {
        return USER_INFO.get(userInfoModEnum);
    }

    public static UserInfo userInfo(UserInfoMod userInfoModEnum, Object loginId, String loginType) {
        return userService(userInfoModEnum).findByUserId(loginId, loginType);
    }

    public static UserInfo userInfo(UserInfoMod userInfoModEnum, Object loginId) {
        return userService(userInfoModEnum).findByUserId(loginId);
    }

    public static UserInfo userInfo(UserInfoMod userInfoModEnum) {
        return userService(userInfoModEnum).findByUserId();
    }

    public static UserInfo userInfo(UserInfoMod userInfoModEnum, String loginId) {
        return userService(userInfoModEnum).findByUserId(loginId);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext.getBeansOfType(FindUserService.class).forEach((k, v) -> USER_INFO.put(v.mod(), v));
    }
}
